// Copyright 2017 Baidu Inc. All Rights Reserved.
// Author: tai xiurong (taixiurong@baidu.com)
//
// Description:upd ssdp
#include <errno.h>
#include "baidu_upnp_test.h"
#include "sockets.h"
#include "minissdp.h"
#include "minirender.h"
#include "upnpdescgen.h"

extern unsigned short port;

void test_gen_xml(){
    //char* gen_root_desc(int* len);
    int len = 0 ;
    //char* root_desc = gen_connection_manager_scpd(&len);
    char* av_desc = gen_av_transport_scpd(&len);
    //char* root_desc = gen_root_desc(&len);
    LOG("root::desc : %s len = %d \n",av_desc,len);
}

void test_sspd_receive(const char* local_ip) {
    LOG("entry test_sspd_receive");
    int sssdp = -1;
    fd_set readset; /* for select() */
    fd_set writeset;
    int max_fd = -1;
    struct timeval timeout;
    timeout.tv_sec = 2;
    sssdp = open_and_conf_ssdp_receive_socket();
    if (sssdp < 0) {
        LOG("create receive socket is failed");
        return;
    }
    add_multicast_membership(sssdp, local_ip);
    while (1) {
        FD_ZERO(&readset);
        FD_ZERO(&writeset);
        if (sssdp >= 0) {
            FD_SET(sssdp, &readset);
            max_fd = max_fd > sssdp ? max_fd : sssdp;//MAX(max_fd, sssdp);
            int ret = select(max_fd + 1, &readset, &writeset, 0, &timeout);
            //LOG("select ret = %d",ret);
            if (ret < 0) {
                LOG("call select failed");
                return;
            }
            //LOG("select 222 ret = %d",ret);
            if (FD_ISSET(sssdp, &readset)) {
                ret = process_ssdp_request(sssdp, local_ip, port);
                if ((ret < 0)) {
                    LOG("process ssdp request fail");
                }
            }
        }
    }
    LOG("leave test_sspd_receive");
}

void test_all_upnp_api(const char* local_ip,const char* mac) {
    upnp_test(local_ip,mac);
}